package com.example.springboot.batch.job;

import org.springframework.batch.core.*;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * @desc
 * @Author wangsh
 * @date 2018/5/14 22:21
 */
@Service
public class JobService {

	@Autowired
	private JobLauncher jobLauncher;

	@Autowired
	private Job job;

	public JobParameters jobParameter;

	@Scheduled(fixedRate = 5000)
	public void execute() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {

		jobParameter = new JobParametersBuilder()//
				.addLong("time", System.currentTimeMillis())//
				.toJobParameters();
		jobLauncher.run(job, jobParameter);
	}


}
